﻿//-----------------------------------------------------------------------
// <copyright file="ClaimsViewerWebPart.cs" company="Danny Jessee">
//     This code is released under the Microsoft Public License (Ms-PL).
//     For more information, see http://facebookwebparts.codeplex.com/license.
// </copyright>
//-----------------------------------------------------------------------
namespace FacebookWebParts
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.IO;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using Facebook;
    using Microsoft.IdentityModel.Claims;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;

    /// <summary>
    /// The Claims Viewer web part displays the current user's claims in a GridView.
    /// </summary>
    [ToolboxItemAttribute(false)]
    public class ClaimsViewerWebPart : WebPart
    {
        /// <summary>
        /// GridView used to display the current user's claims.
        /// </summary>
        private GridView gridView1 = new GridView();

        /// <summary>
        /// Label used to display additional user or error information.
        /// </summary>
        private Label label1 = new Label();

        /// <summary>
        /// Sets up a RowDataBound event handler that adds line breaks to very long strings.
        /// Also stores the Facebook Graph API access token in the SPWeb's property bag for convenience.
        /// </summary>
        /// <param name="e">Default EventArgs parameter.</param>
        protected override void OnLoad(EventArgs e)
        {
            try
            {
                // This event handler only serves to insert line breaks into long strings for display purposes.
                this.gridView1.RowDataBound += new GridViewRowEventHandler(this.GridView1_RowDataBound);

                IClaimsPrincipal claimsPrincipal = Page.User as IClaimsPrincipal;
                IClaimsIdentity claimsIdentity = (IClaimsIdentity)claimsPrincipal.Identity;
                this.gridView1.DataSource = claimsIdentity.Claims;
                this.gridView1.DataBind();
            }
            catch (Exception ex)
            {
                this.label1.Text = ex.Message + ex.StackTrace;
            }

            base.OnLoad(e);
        }

        /// <summary>
        /// Adds the GridView and Label controls to the web part's Controls collection.
        /// </summary>
        protected override void CreateChildControls()
        {
            this.Controls.Add(this.gridView1);
            this.Controls.Add(this.label1);
        }

        /// <summary>
        /// Adjusts the default presentation of results in the GridView control.
        /// Hides columns containing unnecessary info.
        /// </summary>
        /// <param name="sender">Default object parameter.</param>
        /// <param name="e">Default GridRowEventArgs parameter.</param>
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[1].Attributes["style"] = "display:none";
            e.Row.Cells[4].Attributes["style"] = "display:none";

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string txt = e.Row.Cells[0].Text;
                if (!string.IsNullOrEmpty(txt))
                {
                    e.Row.Cells[3].Text = this.AddLineBreaks(e.Row.Cells[3].Text);
                }
            }
        }

        /// <summary>
        /// Utility method to insert line breaks after every 50th character in a long string.
        /// </summary>
        /// <param name="p">A potentially long string.</param>
        /// <returns>A string containing HTML line breaks after every 50th character.</returns>
        private string AddLineBreaks(string p)
        {
            string retVal = string.Empty;

            try
            {
                if (p.Length <= 50)
                {
                    return p;
                }

                for (int i = 0; i < p.Length; i += 50)
                {
                    if (i + 50 <= p.Length)
                    {
                        retVal += p.Substring(i, 50) + "<br/>";
                    }
                    else
                    {
                        retVal += p.Substring(i);
                    }
                }
            }
            catch (Exception ex)
            {
                this.label1.Text += ex.Message + ex.StackTrace;
            }

            return retVal;
        }
    }
}